/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package com.moon.admin.framework.mybatisplus.handler;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.moon.admin.framework.secure.AuthUtils;
import com.moon.admin.framework.secure.domain.AuthInfo;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/**
 * 审计字段自动填充
 * @author moon
 */
@AutoConfigureAfter({MybatisPlusAutoConfiguration.class})
@Configuration
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
	
	@Override
	public void insertFill(MetaObject metaObject) {
		Date now = new Date(System.currentTimeMillis());
		AuthInfo info = AuthUtils.getAuthInfo();
		this.strictInsertFill(metaObject,"createBy",Integer.class,info.getId());
		this.strictInsertFill(metaObject,"updateBy",Integer.class,info.getId());
		this.strictInsertFill(metaObject,"createDept",Integer.class,info.getDeptId());
		this.strictInsertFill(metaObject,"createDept",Integer.class,info.getDeptId());
		this.strictInsertFill(metaObject,"createTime",Date.class,now);
		this.strictInsertFill(metaObject,"updateTime",Date.class,now);
	}

	@Override
	public void updateFill(MetaObject metaObject) {
		Integer userId = AuthUtils.getCurrentUserId();
		Date now = new Date(System.currentTimeMillis());
		this.strictUpdateFill(metaObject,"updateBy",Integer.class,userId);
		this.strictUpdateFill(metaObject,"updateTime",Date.class,now);
	}
}